<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+


class LetterTemplateModel extends AppModel
{

	var $tbl_pref_custom = '';
	var $tables = array('table'=>'letter_template');


	function isPredifined($id) {
		$sql = "SELECT predifined FROM {$this->tbl->table} WHERE id = %d";
		$sql = sprintf($sql, $id);
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return $result->Fields('predifined');
	}
	
	
	function getById($id) {
		$data1 = parent::getById($id);
		$data2 = AppMailParser::getTemplateMsg($data1['letter_key']);
		return $this->getDataMerged($data1, $data2);
	}
	
	
	function getDataMerged($data1, $data2) {
		
		$data = array();
		foreach($data1 as $k => $v) {
			$data[$k] = $v;
			if(empty($v) && isset($data2[$k])) {
				$data[$k] = $data2[$k];
			}
		}
		
		return $data;
	}	
}
?>